pp108 : Sprintf Function for Formatting Dates

Sprintf Function for Formatting Dates

This topic describes the sprintf function for formatting dates.


You can use the sprintf function to customize the date format such that the output (date) is derived in different numeric and text formats to suit the application requirements.

The following table lists the formats supported by the sprintf function for formatting dates:

Table 1. Sprintf Function for Formatting Dates

Format

Description

%M

Month represented by a number between 1 and 12

%MM

Month represented by a number between 01 and 12

%d

Day of the month represented by a number between 1 and 31

%dd

Day of the month represented by a number between 01 and 31

%y

Year represented by a number between 0 and 99

%yy

Year represented by a number between 00 and 99

%yyyy

Year represented by a four-digit number

%MMM

Name of the month abbreviated to three letters in user's locale

%MMMM

Full name of the month in user's locale

%ddd

Name of the day abbreviated to three letters in user's locale

%dddd

Full name of the day in user's locale



Note: The sprintf function for formatting dates can be used in the following ways:

  • sprintf("%D(format)", UTCTime)

  • sprintf("%XD(format)", CordysDateFormat)
    This function expects the
    CordysDateFormat to be between 1970-01-01T00:00:01.0 GMT and 2038-01-19T03:14:08.0 GMT. For example, if the
    CordysDateFormat is given as 1970-01-01T00:00:01.0 GMT, the function returns 1970-Jan-01. Any time beyond these boundaries will return the value as -1.
  • sprintf("%D(format)T%T(format)", UTCTime, UTCTime)

    Function

    Expected Output

    sprintf("%D(%yyyy-%MM-%dd)T%T(%HH:%mm:%ss.0)", 0,0)

    returns the reference date and the reference time -
    1970-01-01T05:30:00.0

    sprintf("%D(%yyyy-%MM-%dd)T%T(%HH:%mm:%ss.0)", 0,utc())

    returns the reference date and current locale time -
    1970-01-01T00:00:01.0

    sprintf("%D(%yyyy-%MM-%dd)T%T(%HH:%mm:%ss.0)", utc(),0)

    returns the current locale date and the reference time -
    2009-01-01T05:30:00.0

    sprintf("%D(%yyyy-%MM-%dd)T%T(%HH:%mm:%ss.0)", utc(),utc())

    returns the current locale date and the current locale time -
    2009-01-01T00:00:01.0

    sprintf("%D(%yyyy-%MM-%dd)T%T(%HH:%mm:%ss.0)", utc(),utcgmt())

    returns the current locale date and the current GMT time -
    2008-12-31T18:30:00.0

    sprintf("%D(%yyyy-%MM-%dd)T%T(%HH:%mm:%ss.0)", utcgmt(),utcgmt())

    returns both current date and time in GMT format -
    2008-12-31T18:30:00.0




    The following are the examples of the output generated by this function:

  • sprintf("%D(%yyyy-%MM-%dd)",0)will display the date as 1970-01-01
  • sprintf("%D(%yyyy-%MMM-%dd)",0)will display the date as 1970-Jan-01
  • sprintf("%XD(%yyyy-%MMM-%dd)","2009-01-01T00:00:01.0") will display the date as 2009-Jan-01.
    To display the current date and time in CordysDateFormat
    sprintf("%D(%yyyy-%MM-%dd)T%T(%HH:%mm:%ss.0)", utc(),utc()) 

The following is the example of the output generated by this function:
sprintf("%D(%yyyy-%MM-%dd)T%T(%HH:%mm:%ss.0)", 0,0) will display the date and time as 1970-01-01T05:30:00.0.